package org.alex.nettychat.netty.learn.learn2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * ChannelInboundHandler的事件传播
 *
 * @author : alex
 */
public class Learn2 {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        /**
         * 1.boss对应IOServer.java中的负责接收新连接的线程，主要负责创建新连接。
         * 2.worker对应IOServer.java中的负责读取数据的线程，主要用于读取数据及业务逻辑处理。
         */
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        serverBootstrap.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                // 数据处理逻辑
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new ChannelInboundHandlerA());
                        channel.pipeline().addLast(new ChannelInboundHandlerB());
                        channel.pipeline().addLast(new ChannelInboundHandlerC());

                        channel.pipeline().addLast(new ChannelOutboundHandlerA());
                        channel.pipeline().addLast(new ChannelOutboundHandlerB());
                        channel.pipeline().addLast(new ChannelOutboundHandlerC());
                    }
                })
                .bind(8000);
    }
}
